﻿#include "mymusic.h"
#include "./ui_mymusic.h"
#include <QPushbutton>
#include <QMouseEvent>
#include <QGraphicsDropShadowEffect>
#include <QDebug>
#include <QJsonObject>
#include <QJsonArray>
#include <QFileDialog>

MyMusic::MyMusic(QWidget* parent)
    : QWidget(parent)
    , ui(new Ui::MyMusic)
{
    ui->setupUi(this);
    connect(ui->quit, &QPushButton::clicked, this, &MyMusic::Quit);
    connect(ui->min, &QPushButton::clicked, this, &MyMusic::miniWindows);//最小化
    connect(ui->max, &QPushButton::clicked, this, &MyMusic::maxWindows);
    connect(ui->volume, &QPushButton::clicked, this, &MyMusic::adjustVolume);
    connect(ui->addLocal, &QPushButton::clicked, this, &MyMusic::addLocalMusic);
    initui();

    connectSignalAndSlots();
}

MyMusic::~MyMusic()
{
    delete ui;
}

void MyMusic::initui() {
    this->setWindowFlag(Qt::FramelessWindowHint);
    this->installEventFilter(this);

    //设置窗口背景透明
    this->setAttribute(Qt::WA_TranslucentBackground);

    //设置窗口阴影
    QGraphicsDropShadowEffect* shadowEffect = new QGraphicsDropShadowEffect(this);
    shadowEffect->setOffset(0, 0);
    shadowEffect->setColor("#000000");
    shadowEffect->setBlurRadius(10);
    this->setGraphicsEffect(shadowEffect);

    //设置主界面功能图标和文本
    ui->recommend->setIconAndText(":/images/rec.png", "推荐音乐", 0);
    ui->radio->setIconAndText(":/images/radio.png", "电台", 1);
    ui->music->setIconAndText(":/images/music.png", "音乐馆", 2);
    ui->like->setIconAndText(":/images/like.png", "我喜欢", 3);
    ui->local->setIconAndText(":/images/local.png", "本地音乐", 4);
    ui->recent->setIconAndText(":/images/recent.png", "最近播放", 5);

    //ui->local->showAnimal();
    //ui->stackedWidget->setCurrentIndex(4);

    srand(time(nullptr));
    ui->recMusicBox->initRecBoxUi(randomPiction(), 1);
    ui->supplyMusicBox->initRecBoxUi(randomPiction(), 2);

    ui->likePage->setCommonPageUI("喜欢", ":/images/ilikebg.png");
    ui->localPage->setCommonPageUI("本地和下载", ":/images/localbg.png");
    ui->recentPage->setCommonPageUI("最近播放", ":/images/recentbg.png");

    volumeTool = new VolumeTool(this);
}

void MyMusic::Quit() {
    close();
}

void MyMusic::miniWindows() {
    showMinimized();
}

void MyMusic::maxWindows() {
    showMaximized();
}

QJsonArray MyMusic::randomPiction() {
    QVector<QString> vecImageName;

    vecImageName << "001.png" << "003.png" << "004.png" << "005.png" << "006.png" << "007.png"
                 << "008.png" << "009.png" << "010.png" << "011.png" << "012.png" << "013.png"
                 << "014.png" << "015.png" << "016.png" << "017.png" << "018.png" << "019.png"
                 << "020.png" << "021.png" << "022.png" << "023.png" << "024.png" << "025.png"
                 << "026.png" << "027.png" << "028.png" << "029.png" << "030.png" << "031.png"
                 << "032.png" << "033.png" << "034.png" << "035.png" << "036.png" << "037.png"
                 << "038.png" << "039.png" << "040.png";

    //随机打乱vecImageName中的顺序
    std::random_shuffle(vecImageName.begin(), vecImageName.end());

    QJsonArray objArray;

    for (int i = 0; i < vecImageName.size(); i++) {
        QJsonObject obj;

        obj.insert("path", ":/images/rec/" + vecImageName[i]);

        QString text = QString("推荐-%1").arg(i + 1, 3, 10, QChar('0'));
        obj.insert("text", text);

        objArray.append(obj);
    }

    return objArray;
}

void MyMusic::connectSignalAndSlots() {
    //处理btClick信号和对应的槽函数
    connect(ui->recommend, &BtForm::btClick, this, &MyMusic::onBtFromClick);
    connect(ui->radio, &BtForm::btClick, this, &MyMusic::onBtFromClick);
    connect(ui->music, &BtForm::btClick, this, &MyMusic::onBtFromClick);
    connect(ui->like, &BtForm::btClick, this, &MyMusic::onBtFromClick);
    connect(ui->local, &BtForm::btClick, this, &MyMusic::onBtFromClick);
    connect(ui->recent, &BtForm::btClick, this, &MyMusic::onBtFromClick);
}

void MyMusic::onBtFromClick(int pageId) {
    QList<BtForm*> btFromList = this->findChildren<BtForm*>();
    for (auto btFrom : btFromList)
    {
        if (btFrom->getpageId() != pageId) {
            //清除按钮背景操作
            btFrom->closeBackground();
        }
    }

    ui->stackedWidget->setCurrentIndex(pageId);
}

bool MyMusic::eventFilter(QObject* watched, QEvent* event)
{
    static QPoint initialPos;
    static bool isDragging = false;
    if (watched == this)
    {
        if (event->type() == QEvent::MouseButtonPress)
        {
            QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event);
            if (mouseEvent->button() == Qt::LeftButton)
            {
                initialPos = mouseEvent->globalPos();
                isDragging = true;
            }
        }
        else if (event->type() == QEvent::MouseMove && isDragging)
        {
            QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event);
            QPoint currentPos = mouseEvent->globalPos();
            QPoint diff = currentPos - initialPos;
            this->move(this->pos() + diff);
            initialPos = currentPos;
        }
        else if (event->type() == QEvent::MouseButtonRelease && isDragging)
        {
            isDragging = false;

        }
    }
    return QWidget::eventFilter(watched, event);
}

void MyMusic::adjustVolume() {
    //获取volume坐标，并转换为屏幕的全局坐标
    QPoint point = ui->volume->mapToGlobal(QPoint(0, 0));
    //计算volumeTool要移动到的位置
    QPoint volumeLeftTop = point - QPoint(volumeTool->width() / 2, volumeTool->height());

    volumeLeftTop.setX(volumeLeftTop.x() + 15);
    volumeLeftTop.setY(volumeLeftTop.y() + 30);

    volumeTool->move(volumeLeftTop);
    volumeTool->show();
}

void MyMusic::addLocalMusic() {
    QFileDialog fileDialog(this, "添加本地音乐", "C:\\", "All Files(*)");
 
    if (fileDialog.exec() == QDialog::Accepted) {
        ui->stackedWidget->setCurrentIndex(4);
        QList<QUrl> fileUrls = fileDialog.selectedUrls();

        musicList.addMusicByUrls(fileUrls);
    }
}